﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using cfg = System.Configuration;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;
namespace BYManageInfoSystem
{
    public partial class Frm_RecvGoodsComp : DockContent
    {
        private DGBY.BLL.SHCompTab m_bllSHCompTab = new DGBY.BLL.SHCompTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectSHCompTabID = 0;
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];

        public Frm_RecvGoodsComp()
        {
            InitializeComponent();
        }

        private void buttonConPMng_Click(object sender, EventArgs e)
        {
            Frm_RecvGoodsCompConP frm_RecvGoodsCompConP = new Frm_RecvGoodsCompConP();
            frm_RecvGoodsCompConP.StartPosition = FormStartPosition.CenterScreen;
            frm_RecvGoodsCompConP.ShowDialog();
        }

        private void ShowAllRecordInDataGridView()
        {
            dataGridViewCompList.DataSource = null;
            m_ds = m_bllSHCompTab.GetAllList();
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewCompList.DataSource = m_ds.Tables[0];
                dataGridViewCompList.Columns[0].Visible = false;
            }
        }

        private void buttonShowAllRecord_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void dataGridViewCompList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strCompID = this.dataGridViewCompList.Rows[this.dataGridViewCompList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectSHCompTabID = Convert.ToInt32(strCompID, 10);
            cellclickSHComp();
        }

        private void cellclickSHComp()
        {
            DGBY.Model.SHCompTab modelSHCompTab = m_bllSHCompTab.GetModel(m_nSelectSHCompTabID);
            if (modelSHCompTab != null)
            {
                this.textBoxCompName.Text = modelSHCompTab.CompanyName;
                this.textBoxAddress.Text = modelSHCompTab.SHCompAddr;
                this.textBoxCompPhone.Text = modelSHCompTab.SHCompTel;
                this.textBoxFax.Text = modelSHCompTab.SHCompFax;
                this.textBoxManagerName.Text = modelSHCompTab.ManagerName;
                this.textBoxTaxPayerIndentityCode.Text = modelSHCompTab.CountPeoCode;
                this.richTextBoxNote.Text = modelSHCompTab.Note;
            }
            else
            {
                this.textBoxCompName.Text = "";
                this.textBoxAddress.Text = "";
                this.textBoxCompPhone.Text = "";
                this.textBoxFax.Text = "";
                this.textBoxManagerName.Text = "";
                this.textBoxTaxPayerIndentityCode.Text = "";
                this.richTextBoxNote.Text = "";
            }
        }

        private void dataGridViewCompList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewCompList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.SHCompTab modelSHCompTab = new DGBY.Model.SHCompTab();

                modelSHCompTab.CompanyName = this.textBoxCompName.Text;
                modelSHCompTab.SHCompAddr = this.textBoxAddress.Text;
                modelSHCompTab.SHCompTel = this.textBoxCompPhone.Text;
                modelSHCompTab.SHCompFax = this.textBoxFax.Text;
                modelSHCompTab.ManagerName = this.textBoxManagerName.Text;
                modelSHCompTab.CountPeoCode = this.textBoxTaxPayerIndentityCode.Text;
                modelSHCompTab.Note = this.richTextBoxNote.Text;

                int nResult = m_bllSHCompTab.Add(modelSHCompTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加", modelSHCompTab.CompanyName);

                    m_nSelectSHCompTabID = m_bllSHCompTab.GetMaxId() - 1;
                    ShowAllRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllSHCompTab.GetModel(m_nSelectSHCompTabID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DGBY.Model.SHCompTab modelSHCompTab = m_bllSHCompTab.GetModel(m_nSelectSHCompTabID);

                modelSHCompTab.CompanyName = this.textBoxCompName.Text;
                modelSHCompTab.SHCompAddr = this.textBoxAddress.Text;
                modelSHCompTab.SHCompTel = this.textBoxCompPhone.Text;
                modelSHCompTab.SHCompFax = this.textBoxFax.Text;
                modelSHCompTab.ManagerName = this.textBoxManagerName.Text;
                modelSHCompTab.CountPeoCode = this.textBoxTaxPayerIndentityCode.Text;
                modelSHCompTab.Note = this.richTextBoxNote.Text;
               // modelSHCompTab.SHCompTabID = m_nSelectSHCompTabID;

                m_bllSHCompTab.Update(modelSHCompTab);

                Frm_Main.Log2DB(this.Text, "修改", modelSHCompTab.CompanyName);
                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
      
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (m_bllSHCompTab.GetModel(m_nSelectSHCompTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllSHCompTab.Delete(m_nSelectSHCompTabID);
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclickSHComp();
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel文件(*.xls)|*.xls";
            string outFile = "";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                outFile = sfd.FileName;
            }
            else
            {
                return;
            }

            string templetFile = templetFilePath + "RecvGoodsComp.xlt";

            DataSet ds = m_bllSHCompTab.GetAllList();
            DataTable dt = ds.Tables[0];
            ExcelBase eB = new ExcelBase(templetFile, outFile);

            try
            {
                eB.DataTableToExcel(dt, 30, 6, 3, "页", 1);
            }
            catch (System.Exception ex)
            {

            }

            eB.Close();
        }

        private void Frm_RecvGoodsComp_Load(object sender, EventArgs e)
        {
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDelete.Visible = true;
            }
        }


    }
}